home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1356 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.1 KB

  1. From: Gordon_B@msn.com (Gordon Thomas)
  2. Subject: Mem Allocation in functions
  3. Date: 13 Jan 96 01:39:38 -0800
  4. Message-ID: <00001a80+00006ba7@msn.com>
  5. Path: news.msn.com!msn.com
  6. Newsgroups: comp.lang.c
  7. Organization: The Microsoft Network (msn.com)
  8.  
  9. I should know the answer, but I don't. I have a function
  10.  
  11. #include <usual stuff>
  12.  void foo(float *stuff, int *length)
  13.    {
  14.     int i;
  15.     stuff = calloc(*length, sizeof(float));
  16. //    diddle around and put numbers in stuff[i]
  17.     return;
  18.    }
  19. //
  20.  int main(void)
  21.   {
  22.    int count;
  23.    float *somenums;
  24.    int np = 20;
  25.    void foo(float *, int *);
  26.    foo(somenums,&np);
  27.     for(count = 0 ; count < np ; count++)
  28.       fprintf(stdout,"%4d %10.2f\n",count, somenums[count] ;
  29.    free(somenums);
  30.    return 0;
  31.   }
  32. My question is: in foo, memory was allocated to the pointer "somenums" and
  33. at the return the array was filled with the correct values.  However,
  34. in main the array is junk NANs, and in fact the pointer is pointing into DS.
  35. On the other hand, if "somenums" is calloc'd in main, correct values are
  36. obtained.
  37. What language spec am I ignorant of that vitiates the above scheme.
  38. Thanx
  39. Gordon
  40.